package shared.file


object FileTransfer {
  
  import java.util.concurrent.TimeUnit
  
  val convertion = TimeUnit.SECONDS.toNanos(1).asInstanceOf[Double]
  
}

class FileTransfer(val name: String, val total: Long) {

  import java.util.concurrent.atomic._
  
  private var soFar = new AtomicLong
  
  val started = System.nanoTime

  def speed = {
    val t = System.nanoTime - started
    FileTransfer.convertion * current.asInstanceOf[Double] / t.asInstanceOf[Double]
  }
  
  def speedFormatted = ByteFormatter.format(speed) + "/s"
  
  def add(l: Long) = soFar.addAndGet(l)

  def current = soFar.get

  def isDone = total >= current
  
  def percentage = 100.0 * (current / total)
  
  def isActive = !isDone
  
}
